// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require(CONFIG.BasePath + "js/enterprise/base");
	var router = require("../hash/pageRouter");
	var footer = require("../footer/footer_gray");
	var top = require("../top/top_main");
	var constant = require("constant");
	var databind = require("databind");
	var dialog = require("dialog");
	var css_loan = require("./myLoan.css");
	var flag = false;
	var tools = require("tools");
	var html_receivables_detail = require("./html_receivables_detail.html");
	var html_loan_detail = require("./html_loan_detail.html");
	var html_expense_detail = require("./html_expense_detail.html");
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var oData = {
		pageIndex: pageIndex,
		pageSize: pageSize,
		totalPageCount: totalPageCount,
		totalRecordCount: totalRecordCount,
		statementBeginDate: "",
		statementEndDate: "",
		statementNumber: "",
		receiptName: "",
		minSourceAmount: "",
		maxSourceAmount: "",
		financeNumber: ""
	};
	var init = function() {
		inquireMoneyStatement();
		router.init();
		initEvent();
	};

	var initEvent = function() {
		if (base.getData("sid") == null) {
			location.href = "../login/login.html";
		};

		$("#receiptSearch").unbind("click").click(function() {
			oData = {
				pageIndex: pageIndex,
				pageSize: pageSize,
				totalPageCount: totalPageCount,
				totalRecordCount: totalRecordCount,
				statementBeginDate: $("#myLoan .statementBeginDate").val(),
				statementEndDate: $("#myLoan .statementEndDate").val(),
				statementNumber: $("#myLoan .statementNumber").val(),
				receiptName: $("#myLoan .receiptName").val(),
				minSourceAmount: $("#myLoan .minSourceAmount").val(),
				maxSourceAmount: $("#myLoan .maxSourceAmount").val(),
				financeNumber: $("#myLoan .financeNumber").val()
			};
			tools.loadList("inquireReceiptMoneyStatementPageList.json", $("#receiptManageBody"), $("#receiptManageTpl"), oData, function() {
				$(".detail").unbind("click").click(function() {});

				$(".verify").unbind("click").click(function() {
					var $this = $(this)
					dialog.showConfirmDialog("核销", "是否确定核销该条账单？", function() {
						verifiCancelMoneyStatement($this.parents("tr").attr("moneystatementId"));
					});
				});
			});
		});
	};

	var inquireMoneyStatement = function() {
		tools.loadList("inquireReceiptMoneyStatementPageList.json", $("#receiptManageBody"), $("#receiptManageTpl"), oData, function() {
			$(".applicationDetail").unbind("click").click(function(){
				router.route("account/accountDetail.html?financeId=" + $(this).parents("tr").attr("financeId"));
			});

			$(".detail").unbind("click").click(function() {
				var statementSourceId = $(this).parents("tr").attr("statementSourceId");
				router.route("account/myLoanDetail.html?loanNoticeId=" + statementSourceId);
			});

			$(".verify").unbind("click").click(function() {
				var $this = $(this)
				dialog.showConfirmDialog("核销", "是否确定核销该条账单？", function() {
					verifiCancelMoneyStatement($this.parents("tr").attr("moneystatementId"));
				});
			});
		});
	};

	//核销
	var verifiCancelMoneyStatement = function(moneyStatementId) {
		base.fnAjax({
			action: "verifiReceiptMoneyStatement.json",
			oData: {
				moneyStatementId: moneyStatementId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					base.alert(1, "核销成功！");
					router.route("account/myLoan.html");
				} else {
					base.alert(2, data.message);
				}
			}
		});
	};

	return {
		init: init
	};
});